package com.yj.jason.jsondemo;

import android.Manifest;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Environment;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.view.LayoutInflaterCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.yj.jason.baselibrary.factory.io.IOHandlerFactory;
import com.yj.jason.baselibrary.fixbug.FixDexManager;
import com.yj.jason.baselibrary.http.EngineCallBack;
import com.yj.jason.baselibrary.http.HttpUtils;
import com.yj.jason.baselibrary.manager.ActivityManager;
import com.yj.jason.baselibrary.utils.SharePreferenceUtil;
import com.yj.jason.baselibrary.view.MyRecyclerView.MySwipeRefreshLayout;
import com.yj.jason.baselibrary.view.MyRecyclerView.SwipeItemClickListener;
import com.yj.jason.baselibrary.view.MyRecyclerView.SwipeMenuRecyclerView;
import com.yj.jason.baselibrary.view.MyRecyclerView.widget.DefaultLoadMoreView;
import com.yj.jason.baselibrary.view.RecyclerView.CommonAdapter.CommonRecyclerAdapter;
import com.yj.jason.baselibrary.view.RecyclerView.CommonAdapter.MuiltTypeSupport;
import com.yj.jason.baselibrary.view.RecyclerView.CommonAdapter.ViewHolder;
import com.yj.jason.baselibrary.view.RecyclerView.Decoration.GirdLayoutItemDecoration;
import com.yj.jason.baselibrary.view.RecyclerView.Decoration.LinearLayoutItemDecoration;
import com.yj.jason.baselibrary.view.RecyclerView.Refresh.DefaultLoadViewCreator;
import com.yj.jason.baselibrary.view.RecyclerView.Refresh.DefaultRefreshCreator;
import com.yj.jason.baselibrary.view.RecyclerView.Refresh.LoadRefreshRecyclerView;
import com.yj.jason.baselibrary.view.RecyclerView.Refresh.RefreshRecyclerView;
import com.yj.jason.baselibrary.view.RecyclerView.WrapRecyclerView;
import com.yj.jason.baselibrary.view.banner.BannerAdapter;
import com.yj.jason.baselibrary.view.banner.BannerView;
import com.yj.jason.baselibrary.view.banner.BannerViewPager;
import com.yj.jason.baselibrary.view.dialog.AlertDialog;
import com.yj.jason.moudlelibrary.activity.BaseSkinActivity;
import com.yj.jason.moudlelibrary.db.DaoSupportFactory;
import com.yj.jason.moudlelibrary.db.IDaoSupport;
import com.yj.jason.moudlelibrary.http.HttpCallBack;
import com.yj.jason.moudlelibrary.view.DefaultNavigation;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import io.reactivex.functions.Consumer;

public class MainActivity extends BaseSkinActivity implements View.OnClickListener{

    private Button btn_banner;
    private Button btn_skin,btn_lzload,btn_tag,btn_kgslide,btn_table,btn_refresh,bt_qqslide,btn_drag,btn_custom_view,btn_time_line,btn_md;

    //测试git提交


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

     //   requestRxPermission();
        btn_banner=(Button)findViewById(R.id.btn_banner);
        btn_skin=(Button)findViewById(R.id.btn_skin);
        btn_lzload=(Button)findViewById(R.id.btn_lzload);
        btn_tag=(Button)findViewById(R.id.btn_tag);
        btn_kgslide=(Button)findViewById(R.id.btn_kgslide);
        btn_table=(Button)findViewById(R.id.btn_table);
        btn_refresh=(Button)findViewById(R.id.btn_refresh);
        bt_qqslide=(Button)findViewById(R.id.bt_qqslide);
        btn_drag=(Button)findViewById(R.id.btn_drag);
        btn_custom_view=(Button)findViewById(R.id.btn_custom_view);
        btn_time_line=(Button)findViewById(R.id.btn_time_line);
        btn_md=(Button)findViewById(R.id.btn_md);

        DefaultNavigation navigationBar = new DefaultNavigation.Builder(this)
                .setLeftIcon(R.mipmap.ic_launcher).setTitle("投稿").create();


        btn_banner.setOnClickListener(this);
        btn_skin.setOnClickListener(this);
        btn_lzload.setOnClickListener(this);
        btn_tag.setOnClickListener(this);
        btn_kgslide.setOnClickListener(this);
        btn_table.setOnClickListener(this);
        btn_refresh.setOnClickListener(this);
        bt_qqslide.setOnClickListener(this);
        btn_drag.setOnClickListener(this);
        btn_custom_view.setOnClickListener(this);
        btn_time_line.setOnClickListener(this);
        btn_md.setOnClickListener(this);



      //  SharePreferenceUtil.getInstance().saveString("jason","majinting").saveString("age","31").commit();


      //  LayoutInflaterCompat.setFactory2();

        IOHandlerFactory.getInstance().getSPHandler().saveString("jason","majinting");


       // fixDexBug();


    }


    FixDexManager fixDexManager;
    //自己的热修复方式
    private void fixDexBug() {
        final File fixFile= new File(Environment.getExternalStorageDirectory(),"jason.dex");

        if(fixFile.exists()){
              fixDexManager=new FixDexManager(this);

            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        fixDexManager.fixDex(fixFile.getAbsolutePath());
                      //  Toast.makeText(MainActivity.this,"bug修复成功",Toast.LENGTH_SHORT).show();
                        IOHandlerFactory.getInstance().getSPHandler().saveString("fix","1");

                    } catch (Exception e) {
                        e.printStackTrace();
                      //  Toast.makeText(MainActivity.this,"bug修复失败",Toast.LENGTH_SHORT).show();
                    }
                }
            }).start();


        }

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_md:
              /*  if(fixDexManager!=null){
                    fixDexManager.deleteAllFixFile();
                }
                IOHandlerFactory.getInstance().getSPHandler().saveString("fix","0");*/
                startActivity(new Intent(this,MDActivity.class));
                break;
            case R.id.btn_time_line:
                startActivity(new Intent(this,TimeLineActivity.class));
                break;

            case R.id.btn_custom_view:
                startActivity(new Intent(this,CustomerActivity.class));
                break;
            case R.id.btn_drag:
                startActivity(new Intent(this,DragHelperActivity.class));
                break;
            case R.id.btn_refresh:
                startActivity(new Intent(this,SmartRefreshActivity.class));
                break;
            case R.id.btn_table:
                startActivity(new Intent(this,TableActivity.class));
                break;
            case R.id.bt_qqslide:
              //  bt_qqslide.setText( IOHandlerFactory.getInstance().getSPHandler().getString("jason"));
                startActivity(new Intent(this,QQSlideActivity.class));
                break;
            case R.id.btn_kgslide:
                startActivity(new Intent(this,KGSlideActivity.class));
                break;
            case R.id.btn_tag:
                startActivity(new Intent(this,TagActivity.class));
                break;
            case R.id.btn_lzload:
                startActivity(new Intent(this,FragmentLoadActivity.class));
                break;
            case R.id.btn_banner:
                startActivity(new Intent(this,BannerViewActivity.class));
                break;
            case R.id.btn_skin:
               startActivity(new Intent(this,SkinActivity.class));

               /* HttpUtils.with(this).cache(true).post().url("http://zengjin.work/agriculture/api/user/member/login.do")
                        .addParam("userName","13698603420")
                        .addParam("password",EncryptUtils.encryptMD5ToString("123456").toLowerCase())
                        .execute(new HttpCallBack<UserLoginBean>(){

                            @Override
                            public void onError(Exception e) {

                            }

                            @Override
                            public void onSuccess(UserLoginBean result) {

                                Toast.makeText(MainActivity.this,result.getMsg(),Toast.LENGTH_SHORT).show();

                            }
                        });*/

                break;
        }

    }


    private void requestRxPermission(){
        RxPermissions rxPermissions = new RxPermissions(this); // where this is an Activity instance

        rxPermissions
                .requestEach(
                        Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Permission>() {
                    @Override
                    public void accept(Permission permission) throws Exception {
                        if (permission.granted) {
                            // LogUtil.i("testRxPermission CallBack onPermissionsGranted() : "+permission.name+
                            //         " request granted , to do something...");

                          /*  if(!IOHandlerFactory.getInstance().getSPHandler().getString("fix").equals("1")) {
                                fixDexBug();
                            }*/

                            fixDexBug();

                            //todo somthing
                            IDaoSupport daoSupport=DaoSupportFactory.getFactory().getDao(Person.class);

                           /* List<Person>persons=new ArrayList<>();

                            for(int i=0;i<10;i++){
                                Person person=new Person("jason",23+i);
                                persons.add(person);
                            }

                            daoSupport.insert(persons);*/

                           List<Person>list=daoSupport.querySupport().SELECTION("age>?").SELECTIONARGS("25").query();

                           for(Person person:list){
                               Log.i("tag",person.getAge()+"");
                           }



                        }
                        else if (permission.shouldShowRequestPermissionRationale) {
                            // LogUtil.e("testRxPermission CallBack onPermissionsDenied() : " + permission.name + "request denied");
                            // ToastUtil.showShort(instance, "拒绝权限，等待下次询问哦");
                            //todo request permission again
                        }
                        else {
                            // LogUtil.e("testRxPermission CallBack onPermissionsDenied() : this " + permission.name + " is denied " +
                            //        "and never ask again");
                            //  ToastUtil.showShort(instance, "拒绝权限，不再弹出询问框，请前往APP应用设置中打开此权限");
                            //todo nothing
                        }
                    }
                });

    }

}
